package com.zxx.autohomenavi.ziru_svg;

import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.text.TextUtils;

import com.zxx.autohomenavi.R;
import com.zxx.autohomenavi.base.BaseActivity;
import com.zxx.autohomenavi.fragment.TitleFragment;
import com.zxx.autohomenavi.ziru_svg.ZiroomTabView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

/**
 * SVG动画
 * @author zxx on 2021/8/23
 */
public class ZiroomActivity extends BaseActivity {

    private Fragment lastFragment;
    private ZiroomTabView ll_tab_view;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_svga_ziroom;
    }

    @Override
    protected void initView() {
        ll_tab_view = findViewById(R.id.ll_tab_view);
    }

    @Override
    protected void onCreateEnd(Bundle savedInstanceState) {
        super.onCreateEnd(savedInstanceState);
        //Log.e("ZiroomActivity", "onCreateEnd");

        ll_tab_view.setOnTabSelectedListener(new ZiroomTabView.ZiroomTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switchFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        if (savedInstanceState != null) {
            lastFragment = getSupportFragmentManager().getFragment(savedInstanceState, "LAST_FRAGMENT");
        }
        if (lastFragment != null) {
            int lastPosition = getFragmentPosition(lastFragment);
            //Log.e("ZiroomActivity:", lastPosition + "<-1-");

            if (lastPosition != -1) {
                ll_tab_view.setSelectedPosition(lastPosition, false);
            }
            //Log.e("onCreateEnd", "lastPosition:$" + lastPosition + ",lastFragment=$" + lastFragment);
        } else {
            Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
                @Override
                public boolean queueIdle() {
                    //Log.e("ZiroomActivity:", lastFragment + "<-2-");
                    ll_tab_view.setSelectedPosition(0, true);
                    return false;
                }
            });
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        //Log.e("ZiroomActivity", lastFragment + "<-0-");
        if (lastFragment != null) {
            getSupportFragmentManager().putFragment(outState, "LAST_FRAGMENT", lastFragment);
        }
        super.onSaveInstanceState(outState);
    }

    /**
     * 获取Fragment对应的位置
     */
    private int getFragmentPosition(Fragment fragment) {
        int position = -1;
        if (fragment != null) {
            String tag = fragment.getTag();
            if (!TextUtils.isEmpty(tag)) {
                try {
                    position = Integer.parseInt(tag);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        return position;
    }

    /**
     * 切换Fragment到指定位置
     */
    private void switchFragment(int position) {
        int lastPosition = -1;
        if (lastFragment != null) {
            lastPosition = getFragmentPosition(lastFragment);
            if (lastPosition == position) {
                return;
            }
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment newFragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(position));
        if (newFragment == null) {
            switch (position) {
                case 0:
                    newFragment = TitleFragment.getInstance(getString(R.string.ziroom_tab_home));
                    break;
                case 1:
                    newFragment = TitleFragment.getInstance(getString(R.string.ziroom_tab_search));
                    break;
                case 2:
                    newFragment = TitleFragment.getInstance(getString(R.string.ziroom_tab_service));
                    break;
                case 3:
                    newFragment = TitleFragment.getInstance(getString(R.string.ziroom_tab_lvju));
                    break;
                case 4:
                    newFragment = TitleFragment.getInstance(getString(R.string.ziroom_tab_my));
                    break;
            }
        }
        if (newFragment != null && !newFragment.isAdded()) {
            ft.add(R.id.fl_container, newFragment, String.valueOf(position));
        }
        if (lastFragment != null) {
            ft.hide(lastFragment);
        }
        if (newFragment != null) {
            ft.show(newFragment);
        }
        ft.commitAllowingStateLoss();
        lastFragment = newFragment;
    }
}
